package ca.uwindsor.cs.deepweb.utility.documentanalysis;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;
import ca.uwindsor.cs.deepweb.estimation.RandomWordSelector;

public class WordDocumentFrequencyExtractor {
	protected String index;

	protected String[] words;

	public WordDocumentFrequencyExtractor(String index, String[] words) {
		super();
		this.index = index;
		this.words = words;
	}

	public int[] getDocumentFrequency() {
		int[] df = new int[words.length];
		QueryParser queryparser = new QueryParser(
				FilesContentIndexer.FIELD_CONTENT, new StandardAnalyzer());
		Query query = null;
		Hits hits = null;
		IndexSearcher searcher;
		try {
			searcher = new IndexSearcher(index);
			for (int i = 0; i < words.length; i++) {
				query = queryparser.parse(words[i]);
				hits = searcher.search(query);
				df[i] = hits.length();
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		}
		return df;
	}

	public static void main(String[] args) {
		if (args.length != 3) {
			System.out
					.println("java -jar LuceneIndexReader.jar indexDir outputDir");
			System.exit(-1);
		}
		try {
			RandomWordSelector selector = new RandomWordSelector(args[1],
					"UTF-8");
			String[] words = selector.getWordList().toArray(
					new String[selector.getWordList().size()]);
			WordDocumentFrequencyExtractor e = new WordDocumentFrequencyExtractor(
					args[0], words);
			int df[] = e.getDocumentFrequency();
			
			FileOutputStream fp = new FileOutputStream(args[2]);
			String line;
			for (int i=0;i<words.length;i++) {
				line = words[i] + "\t" + df[i] + "\t" + df[i] + "\n";
				fp.write(line.getBytes("UTF-8"));
			}
			fp.flush();
			fp.close();
		} catch (CorruptIndexException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
